package com.ynb.lighterbuffer;

public class ObjectSchema implements ISchema<Object>{
	@SuppressWarnings("unchecked")
	@Override
	public void serialize(Object o,BinaryWriter writer) throws Exception{
		if (o == null) {
			writer.writeNull(true);
			return;
		}
		writer.writeNull(false);
		if(o.getClass().equals(Object.class)){
			writer.writeInt(0);
			return;
		}else{
			ISchema<Object> schema = (ISchema<Object>)SchemaPool.getSchema(o.getClass());
			BinaryWriter writer_= BinaryIOBuilderFactory.buildWriter();
			schema.serialize(o,writer_);
			byte[] data=writer_.toArray();
			writer.writeInt(data.length);
			writer.writeBytes(data);
		}
	}
	
	@Override
	public Object deSerialize(Class<Object> type,BinaryReader reader) throws Exception{
		if(reader.readNull()){
			return null;
		}
		if(reader.readBool()){
			return new Object();
		}
		int len=reader.readInt();
		return reader.readBytes(len);
	}
}